<template>
  <el-row class="login-content" type="flex" justify="center" align="middle">
    <el-col :span="8">
      <div class="login-box">
        <h2>欢迎登录</h2>
        <!-- 表单验证三要素：
        1.rules属性表示验证规则对象
        2.要有model属性，绑定表单数据对象
        3.要有prop属性，值为验证的字段
         -->
        <el-form
          :model="form"
          ref="form"
          :rules="rules"
          label-width="80px"
          :inline="false"
          size="normal"
        >
          <el-form-item label="用户名" prop="username">
            <el-input el-icon-user clearable v-model="form.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              clearable
              show-password
              v-model="form.password"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit('form')">登录</el-button>
            <el-button @click="resetForm('form')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-col>
  </el-row>
</template>

<script>
export default {
  name: "login",
  data() {
    return {
      form: {
        username: "admin",
        password: "123456",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
         this.$store.dispatch("login", this.form);
   
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped>
.login-content {
  width: 100vw;
  height: 100vh;
  background-color: #2b5b6b;
}
.login-box {
  min-height: 400px;
  background-color: #fff;
  padding: 20px;
  box-sizing: border-box;
  border-radius: 10px;
}
h2 {
  text-align: center;
  padding-bottom: 30px;
}
</style>